﻿Public Class frmCodeUnit
    Dim WithEvents Refresher As New Timer With {.Enabled = True}

    Public Class Editor
        Inherits Drawing.Design.UITypeEditor

        Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Drawing.Design.UITypeEditorEditStyle
            Return Drawing.Design.UITypeEditorEditStyle.Modal
        End Function


        Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext,
        ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

            Dim editor_service As Windows.Forms.Design.IWindowsFormsEditorService =
                CType(provider.GetService(GetType(Windows.Forms.Design.IWindowsFormsEditorService)), Windows.Forms.Design.IWindowsFormsEditorService)

            If editor_service Is Nothing Then Return value

            Dim IsProperty As Reflection.PropertyInfo = context.Instance.GetType.GetProperty(context.PropertyDescriptor.Name)
            Dim MyString = IsProperty.GetValue(context.Instance, Nothing)


            'Dim A = IsProperty.Attributes
            ''Dim IsForm As New frmCodeUnit(MyString, context.PropertyDescriptor.Name)

            'Dim TA = IsProperty.GetCustomAttributesData()
            'Dim TA1 = IsProperty.GetCustomAttributes(True)


            Dim GetForm As New frmCodeUnit(CType(MyString, Generator.Code_Units.Property))

            editor_service.ShowDialog(GetForm)

            Return GetForm.GetValue

            'Return context.Instance
        End Function

    End Class

    Sub New()
        InitializeComponent()
    End Sub

    Sub New(ByVal Code As Generator.Code_Units.Property)
        InitializeComponent()
        Me.cbxScope.Text = Code.Scope.ToString
        Me.txtPropertyName.Text = Code.Name
        Me.cbxTypeName.Text = Code.Data_Type
        Me.chkShared.Checked = Code.Shared
        Me.chkIsReadOnly.Checked = Code.ReadOnly_Static
        Me.chkOneLine.Checked = Code.OneLine
    End Sub


    Public Shared Function [New](ByVal Code As Generator.Code_Units.Property) As Generator.Code_Units.Property
        Dim _Form As New frmCodeUnit
        _Form.cbxScope.Text = Code.Scope.ToString
        _Form.txtPropertyName.Text = Code.Name
        _Form.cbxTypeName.Text = Code.Data_Type
        _Form.chkShared.Checked = Code.Shared
        _Form.chkIsReadOnly.Checked = Code.ReadOnly_Static
        _Form.chkOneLine.Checked = Code.OneLine
        _Form.ShowDialog()
        Return _Form.GetValue


    End Function

    Public Function GetValue() As Generator.Code_Units.Property
        Dim Scope As Generator.BaseCode.Visibility_Scope = [Enum].Parse(GetType(Generator.BaseCode.Visibility_Scope), cbxScope.Text)
        Return New Generator.Code_Units.Property With {.Data_Type = cbxTypeName.Text, .Name = txtPropertyName.Text, .OneLine = chkOneLine.Checked,
                                                       .ReadOnly_Static = chkIsReadOnly.Checked, .Scope = Scope, .Shared = chkShared.Checked}
    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Code As New My.Templates.TemplateA(strScope:=cbxScope.Text, bolIsShared:=chkShared.Checked, bolIsReadOnly:=chkIsReadOnly.Checked,
                                               bolIsSetterPrivate:=chkIsSetterPrivate.Checked, strPropertyName:=txtPropertyName.Text,
                                               strBackingField:=txtBacking.Text, strTypeName:=cbxTypeName.Text, strRaisePropertyChangedMethodName:="")



        Me.TextBox1.Text = Code.TransformText
    End Sub



    Private Sub Refresher_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Refresher.Tick
        Dim Code As New My.Templates.TemplateA(strScope:=cbxScope.Text, bolIsShared:=chkShared.Checked, bolIsReadOnly:=chkIsReadOnly.Checked,
                                       bolIsSetterPrivate:=chkIsSetterPrivate.Checked, strPropertyName:=txtPropertyName.Text,
                                       strBackingField:=txtBacking.Text, strTypeName:=cbxTypeName.Text, strRaisePropertyChangedMethodName:="")



        Me.TextBox1.Text = Code.TransformText
    End Sub
End Class